<template>
  <div>
    <div class="gui-padding bgf">
      <div class="mb15">
        <a-breadcrumb>
          <a-breadcrumb-item>公司管理</a-breadcrumb-item>
          <a-breadcrumb-item>船舶职位管理</a-breadcrumb-item>
        </a-breadcrumb>
      </div>
      <div class="flex justify-content-between mb10">
        <div class="flex">
          <div>
          <span class="mr5"><a-button type="primary" size="small" :loading="addLoading" @click="handleAddOpen"> <template
              #icon><PlusOutlined/> </template>添加船舶职位</a-button></span>
          </div>
        </div>
        <div>
          <a-form layout="inline" autocomplete="off" ref="searchFormRef" :model="queryParams">
            <a-form-item label="关键词" name="keyword">
              <a-input v-model:value.trim="queryParams.keyword" size="small" style="width: 160px"
                       placeholder="请输入船舶职位名称" allow-clear/>
            </a-form-item>
            <a-form-item>
              <div class="flex">
                <a-button type="primary" size="small" @click="handleSearch">
                  <SearchOutlined/>
                  搜索
                </a-button>
                <a-button type="primary" danger size="small" @click="handleReset" class="ml10">
                  <UndoOutlined/>
                  重置
                </a-button>
              </div>
            </a-form-item>
          </a-form>
        </div>
      </div>
      <a-table
          :columns="columns"
          rowClassName="items"
          :loading="tableLoading"
          :scroll="{ x: 'calc(100vw - 326px)', y: 'calc(100vh - 245px)' }"
          rowKey="id"
          :pagination="false"
          :data-source="tableData"
          style="height:calc(100vh - 215px)"
          size="small" sticky bordered>
        <template #bodyCell="{ column,text,record,index }">
          <template v-if="column.dataIndex==='username'">
            <div class="flex">
              <img :src="record.avatar" class="avatar" style="width: 24px;height: 24px;border-radius: 50%;">
              <span class="ml10">{{ record.username }}</span>
            </div>
          </template>
          <template v-if="column.dataIndex==='zhuangTai'">
            <a-switch v-model:checked="record.zhuangTai" @change="handleChangeUpdate(record,'zhuangTai')"
                      :checked-value="1"
                      :un-checked-value="0"
                      checked-children="激活中"
                      un-checked-children="未激活"/>
          </template>
          <template v-if="column.dataIndex === 'operation'">
            <div class="editable-row-operations"><span class="mr5">
              <a-button type="primary"  size="small" @click="handleEditOpen(record)"> <template
                  #icon><EditOutlined style="font-size:12px"/></template>编辑</a-button>
            </span>
              <span class="mr5">
                  <a-popconfirm title="你确定删除吗?" @confirm="handleDelete(record.id)">
                   <a-button type="primary" size="small" danger> <template #icon><CloseOutlined
                       style="font-size:12px"/></template>删除</a-button>
                  </a-popconfirm>
            </span>
            </div>
          </template>
        </template>
      </a-table>
      <div class="flex  mt20">
        <a-pagination
            v-model:current="queryParams.pageNo"
            v-model:page-size="queryParams.pageSize"
            @change="handleChangePageNo"
            :page-size-options="pageSizeOptions"
            :show-total="total => `共 ${total} 项`"
            show-size-changer
            :total="total"/>
      </div>
      <add-company-job ref="addRef" @success="handleLoadData"></add-company-job>
    </div>
  </div>
</template>
<script setup>
import AddCompanyJob from "./compoments/AddCompanyJob.vue";

const addRef = ref(null)
const searchFormRef = ref(null)
const tableLoading = ref(false)
const addLoading = ref(false)
const total = ref(0)
const tableData = ref([]);
const pageSizeOptions = ref(['15','20', '30', '40', '50']);
const queryParams = reactive({
  pageNo: 1,
  pageSize: 15,
  status: null,
  keyword: ''
})

const columns = [
  {
    title: '序号',
    width: 80,
    dataIndex: 'index',
    align: "center",
    fixed: 'left',
    customRender: ({text, record, index}) => index + 1
  },
  {title: '船舶职位',  dataIndex: 'zhiWeiMing', align: "center"},
  {title: '排序号',  dataIndex: 'xuHao', align: "center"},
  {title: '状态', dataIndex: 'zhuangTai',  align: "center"},
  {
    title: '操作',
    dataIndex: 'operation',
    align: "center",
    width: 200,
  },
];


/**
 * 查询列表数据
 */
const handleLoadData = async () => {
  try {
    tableLoading.value = true
    const resp = await useLoadChuanBoZhiWeiApi(queryParams)
    tableLoading.value = false
    tableData.value = resp.data.records
    total.value = resp.data.total
  } catch (e) {
    useMessage().error(e.msg)
  }
}

// 分页查询
const handleChangePageNo = (pno, psize) => {
  queryParams.pageNo = pno;
  queryParams.pageSize = psize;
  handleLoadData()
}

// 搜索
const handleSearch = () => {
  queryParams.pageNo = 1;
  handleLoadData()
}

const handleReset = () => {
  searchFormRef.value.resetFields()
  handleLoadData()
}


// 修改状态
const handleChangeUpdate = async (record, field) => {
  await useUpdateStatusChuanBoZhiWeiApi({id: record.id, field: field, value: record[field]})
  useMessage().success("操作成功!")
}

/**
 * 删除
 * @param id
 * @returns {Promise<void>}
 */
const handleDelete = async (id) => {
  try {
    await useDelChuanBoZhiWeiApi(id)
    notification.destroy()
    notification.success({
      "message": "提示",
      "description": "删除成功"
    })
    handleLoadData()
  }catch (e){
    useMessage().error(e.msg)
  }

}

// 编辑
const handleEditOpen = (record) => {
  addRef.value.handleOpen({...record})
}

// 添加
const handleAddOpen = () => {
  addRef.value.handleOpen()
}


onMounted(() => {
  handleLoadData()
})

</script>